﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class PostClass : System.Web.UI.Page
    {
        private int editid = 0;
        private int delid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterSchool"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    TClassInfo classInfo = new TClassInfo();
                    classInfo = TClass.GetSingleInfo(editid);
                    ClassName.Text = classInfo.Name.ToString();
                    BindGrade(classInfo.GradeId.ToString());
                }
                else
                {
                    BindGrade("");
                }
                if (delid > 0)
                {
                    if (!UserValidate.CheckLogin(this.Request, "DelSchool"))
                    {
                        Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                        Response.Redirect("login.aspx");
                        Response.End();
                    }
                    TClass.DelInfo(delid);
                    Response.Redirect("AdminSchool.aspx");
                    return;
                }
            }
        }
        protected void BindGrade(string selected)
        {
            IList<GradeInfo> list = Grade.GetList();
            foreach (GradeInfo l in list)
            {
                l.Name = l.SchoolName + "[" + l.Name + "]";
            }
            this.drpGrade.DataTextField = "Name";
            this.drpGrade.DataValueField = "Id";
            this.drpGrade.DataSource = list;
            this.drpGrade.DataBind();
            if (!PageValidate.IsEmpty(selected))
            {
                this.drpGrade.SelectedValue = selected;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ClassName.Text.Trim() == "")
            {
                Utility.ShowJsAlert("班级名称不能为空", this);
                return;
            }
            TClassInfo classInfo = new TClassInfo();
            classInfo.Name = ClassName.Text.Trim();
            classInfo.GradeId = int.Parse(this.drpGrade.SelectedItem.Value);
            classInfo.GradeName = this.drpGrade.SelectedItem.Text;
            if (editid > 0)
            {
                classInfo.Id = editid;
            }
            TClass.Save(classInfo);
            Utility.ReloadTopPage(this);
        }
    }
}
